const log = require('../../utils/log');
const gameCfg = require('../../config/gameCfg');
const hdcjMd = require('./hdcjMd');
const {Respones} = require("../../config/gameCfg");
const  redsiBase = require('../../common/RedisBase');
const dbBase = require('../../common/DbBase');
const hallReset = require('../hall/hallReset');

//活动------每日抽奖

class Hdchoujiang {

    constructor() {

    }

    static getInstance(){
        if(!Hdchoujiang.instance){
            Hdchoujiang.instance = new Hdchoujiang();
            return Hdchoujiang.instance;
        }else{
            return Hdchoujiang.instance;
        }
    }

    //活动抽奖配置
    async getChouJiangCfg(data,ctype,client){

        let cfg = hdcjMd.getCjCfg();

        let ret = {
            type:ctype,
            data:{
                rewards:[], //具体的奖励数据
                //单次抽奖消耗球币数
                price:cfg.price,
                //单抽次数
                optNum1: 1,
                //5连抽
                optNum5: 5
            }
        }

        let rds = cfg.rewards;
        for (let i = 0; i < rds.length; i++) {
            let item = {
                idx: rds[i].idx,
                rewardType:rds[i].rdType,
                rewardNum:rds[i].rdNun,
            }
            ret.data.rewards.push(item);
        }

        client.send(JSON.stringify(ret));
    }

    //抽奖操作
    async getChouJiangOpt(data,ctype,client){
        let uid = data.userId;
        let count = data.count; //单次操作抽奖几次， count = 1表示单抽， count == 5 表示 5连抽
        let free = data.isFree; //1 是扣费 2 是看广告免费

        let ret = {
            type: ctype,
            data:{
                code:Respones.OK,//code = 1，表示抽奖成功 code= 2 表示球币不足
                idxs:[],
            }
        }

        let cfg = hdcjMd.getCjCfg();
        let rds = cfg.rewards;
        let price = cfg.price; //消耗抽奖券

        if(free === 1){ //扣费
            //计算消耗
            let user = await redsiBase.getUserInfo(uid);
            if(user.ticket - price*count >= 0){
                ret.data.code = Respones.OK;
                let list = await redsiBase.getUserList();
                for (let i = 0; i < list.length; i++) {
                    let u = JSON.parse(list[i]);
                    if(u.userId === uid){
                        u.ticket -= price*count;
                        //更新redis用户数据
                        await redsiBase.updateUserist(i,JSON.stringify(u));
                        //更新到mysal
                        await dbBase.updateUserTicketNum(uid, u.ticket);
                        break;
                    }
                }
                //计算奖品
                let rdsArr = [];
                for (let i = 0; i < count; i++) {
                    let result = hdcjMd.weightedRandom(rds);
                    ret.data.idxs.push(result.idx);
                    rdsArr.push(result);
                }

                //将奖励的数据添加到用户数据上
                let nunqiu = 0;
                let numQuan = 0;
                for (let i = 0; i < rdsArr.length; i++) {
                    let tmp = rdsArr[i];
                    if(tmp.rdType === gameCfg.RewardType.Type_Quan){ //抽奖券
                        numQuan += tmp.rdNun
                    }else if(tmp.rdType === gameCfg.RewardType.TYpe_QiuNum){ //球币
                        nunqiu += tmp.rdNun
                    }
                }
                let uList = await redsiBase.getUserList();
                for (let i = 0; i < uList.length; i++) {
                    let u = JSON.parse(uList[i]);
                    if(u.userId === uid){
                        u.ticket = u.ticket*1 + numQuan;
                        u.ballNum = u.ballNum*1 +nunqiu;
                        await redsiBase.updateUserist(i,JSON.stringify(u));
                        await dbBase.updateUserTicketNum(uid,u.ticket);
                        await dbBase.updateUserBallNum(uid,u.ballNum);
                        break;
                    }
                }
            }else{ //球币不足
                ret.data.code = Respones.QIU_NAN;
            }
        }else if(free === 2){ //广告免费
            //扣除广告次数
            let ud = await redsiBase.getResetInfoByUid(uid);
            if(ud){
                if(ud.freeCjCur >= ud.freeCjMax){
                    ret.data.code = Respones.FREE_COUNT_NAN;
                }else{
                    ud.freeCjCur++;
                    await hallReset.updateResetData(uid,ud);

                    ret.data.code = Respones.OK;
                    //计算奖品
                    let rdsArr = [];
                    for (let i = 0; i < count; i++) {
                        let result = hdcjMd.weightedRandom(rds);
                        ret.data.idxs.push(result.idx);
                        rdsArr.push(result);
                    }

                    //将奖励的数据添加到用户数据上
                    let nunqiu = 0;
                    let numQuan = 0;
                    for (let i = 0; i < rdsArr.length; i++) {
                        let tmp = rdsArr[i];
                        if(tmp.rdType === gameCfg.RewardType.Type_Quan){ //抽奖券
                            numQuan += tmp.rdNun
                        }else if(tmp.rdType === gameCfg.RewardType.TYpe_QiuNum){ //球币
                            nunqiu += tmp.rdNun
                        }
                    }
                    let uList = await redsiBase.getUserList();
                    for (let i = 0; i < uList.length; i++) {
                        let u = JSON.parse(uList[i]);
                        if(u.userId === uid){
                            u.ticket = u.ticket*1 + numQuan;
                            u.ballNum = u.ballNum*1 +nunqiu;
                            await redsiBase.updateUserist(i,JSON.stringify(u));
                            await dbBase.updateUserTicketNum(uid,u.ticket);
                            await dbBase.updateUserBallNum(uid,u.ballNum);
                            break;
                        }
                    }
                }
            }
        }

        log.info('===getChouJiangOpt end===');
        client.send(JSON.stringify(ret))
    }
}

module.exports = Hdchoujiang.getInstance();
